﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Anchor.Miscellaneous;
using Anchor.Sockets;
using Anchor.Miscellaneous.Habbo;

namespace Anchor.Miscellaneous
{
    class Packets
    {
        public Request mRequest;
        public static void Invoke(SocketRequest Socket, int id, string packet)
        {
            try
            {
                Packets p = new Packets();

                foreach (UserPackets mContent in Bootup.Packets)
                {
                    if (mContent.mRecId == id)
                    {
                        p.mRequest = Request.Parse(packet);

                        string Type = Bootup.PacketTypes[mContent.mTypeId];

                        switch (Type)
                        {
                            case "sso":
                                p.DoSSO(Socket, mContent.mRecId, mContent.mSentId, mContent.mPacket);
                                break;
                            case "dologin":
                                p.DoLogin(Socket, mContent.mRecId, mContent.mSentId, mContent.mPacket);
                                break;
                            case "senddata":
                                p.Send(Socket, mContent.mRecId, mContent.mSentId, mContent.mPacket);
                                break;
                            case "loggedin":
                                p.LoggedIn(Socket, mContent.mRecId, mContent.mSentId, mContent.mPacket);
                                break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        private void DoSSO(SocketRequest Socket, int RecId, int SendId, string SendPacket)
        {
            string SSOticket = mRequest.PopString();

            using (DatabaseClient dbClient = Environment.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("sso", SSOticket);
                if (dbClient.ReadBoolean("SELECT id FROM users WHERE ssoticket = @sso;"))
                {
                    dbClient.ExecuteQuery("UPDATE users SET SockId = '" + Socket.SocketID + "' WHERE username = '"
                        + dbClient.ReadString("SELECT username FROM users WHERE ssoticket = @sso;") + "'");

                    //Send packets
                    Socket.SendMessage(Replace(Socket, false, hEncoding.encodeB64(SendId) + SendPacket));
                }
            }
        }
        private void DoLogin(SocketRequest Socket, int RecId, int SendId, string SendPacket)
        {
            string User = mRequest.PopString();
            string PW = mRequest.PopString();

            Console.WriteLine(User);
            using (DatabaseClient dbClient = Environment.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("usr", User);
                dbClient.AddParamWithValue("pw", PW);
                if (dbClient.ReadBoolean("SELECT id FROM users WHERE username = @usr AND password = @PW;"))
                {
                    dbClient.ExecuteQuery("UPDATE users SET SockId = '" + Socket.SocketID + "' WHERE username = '"
                        + dbClient.ReadString("SELECT username FROM users WHERE username = @usr;") + "'");

                    //Send packets
                    Socket.SendMessage(Replace(Socket, false, hEncoding.encodeB64(SendId) + SendPacket));
                }
            }
        }
        private void Send(SocketRequest Socket, int RecId, int SendId, string SendPacket)
        {
            Socket.SendMessage(Replace(Socket, false, hEncoding.encodeB64(SendId) + SendPacket));
        }
        private void LoggedIn(SocketRequest Socket, int RecId, int SendId, string SendPacket)
        {
            Socket.SendMessage(Replace(Socket, true, hEncoding.encodeB64(SendId) + SendPacket));
        }
        public HabboManager GetHabbo(SocketRequest S)
        {
            DataRow row;
            HabboManager Habbo = new HabboManager();

            using (DatabaseClient dbClient = Environment.GetDatabase().GetClient())
            {
                row = dbClient.ReadDataRow("SELECT * FROM `users` WHERE `SockId` =  '" + S.SocketID + "'");
            }
            return Habbo.Init(row);
        }
        private string Replace(SocketRequest S, bool DoDetails, string Packet)
        {
            Packet = Packet.Replace("{DateTimeNow}", Convert.ToString(DateTime.Now));
            if (DoDetails)
            {
                Packet = Packet.Replace("{UserId}", hEncoding.encodeWired(GetHabbo(S).ID));
                Packet = Packet.Replace("{Username}", GetHabbo(S).Username);
                Packet = Packet.Replace("{Figure}", GetHabbo(S).Figure);
                Packet = Packet.Replace("{Motto}", GetHabbo(S).Motto);
                Packet = Packet.Replace("{Credits}", Convert.ToString(GetHabbo(S).Credits));
                Packet = Packet.Replace("{SockId}", Convert.ToString(S.SocketID));
            }
            return Packet;
        }
    }
}
